package com.contract.spirit.func.dialog.base

import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.contract.commonlib.util.ext.setOnSingleClickListener
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.contract.spirit.R
import com.contract.spirit.bean.entity.DialogBean
import com.contract.spirit.func.callback.OnPopDataBindListener
import com.lxj.xpopup.core.BasePopupView

/**
 * @author: witness
 * created: 2021/9/24
 * desc:
 */
class BasePopDataBindImpl : OnPopDataBindListener<DialogBean> {
    private var dialogClickCallBack: OnDialogCallBack? = null
    override fun onBindData(
        popupView: BasePopupView,
        viewHolder: BaseViewHolder,
        commonDialogInfo: DialogBean
    ) {
        with(commonDialogInfo) {
            val btnRight: TextView? = viewHolder.getViewOrNull(R.id.btn_right)
            btnRight?.apply {
                if (!TextUtils.isEmpty(rightBtnText)) {
                    text = rightBtnText
                    visibility = View.VISIBLE
                } else {
                    visibility = View.GONE
                }
                setOnSingleClickListener {
                    dialogClickCallBack?.onRightBtnClick()
                }
            }

            val btnLeft: TextView? = viewHolder.getViewOrNull(R.id.btn_left)
            btnLeft?.apply {
                if (!TextUtils.isEmpty(leftBtnText)) {
                    text = leftBtnText
                    visibility = View.VISIBLE
                } else {
                    visibility = View.GONE
                }
                setOnSingleClickListener {
                    dialogClickCallBack?.onLeftBtnClick()
                }
            }
            val tvTitle: TextView? = viewHolder.getViewOrNull(R.id.tv_title)
            tvTitle?.apply {
                if (!TextUtils.isEmpty(title)) {
                    tvTitle.text = title
                }
            }

            val tvSubTitle: TextView? = viewHolder.getViewOrNull(R.id.tv_subTitle)
            tvSubTitle?.apply {
                if (TextUtils.isEmpty(subTitle)) {
                    visibility = View.GONE
                } else {
                    visibility = View.VISIBLE
                    text = subTitle
                }
            }

            val ivClose: ImageView? = viewHolder.getViewOrNull(R.id.iv_close)
            ivClose?.apply {
                if (isShowClose) {
                    visibility = View.VISIBLE
                    setOnSingleClickListener {
                        popupView.dismiss()
                        dialogClickCallBack?.onClose()
                    }
                } else {
                    visibility = View.GONE
                }
            }
        }


    }


     interface OnDialogCallBack {
        fun onRightBtnClick() {}
        fun onLeftBtnClick() {}
        fun onClose() {}
    }

    fun setOnDialogListener(callBack: OnDialogCallBack) {
        dialogClickCallBack = callBack
    }
}